package br.com.imagechess.player;

import java.util.HashMap;
import java.util.Map;

public class ChessGame {
	Map<String,Piece> gameTable = new HashMap<String,Piece>();
	private static Piece VOID = new Piece("", true);
	
	public ChessGame() {
		initiateGame();
	}

	private void initiateGame() {
		gameTable.put("A1", new Piece("Rock", true));
		gameTable.put("B1", new Piece("Knight", true));
		gameTable.put("C1", new Piece("Bishop", true));
		gameTable.put("D1", new Piece("Queen", true));
		gameTable.put("E1", new Piece("King", true));
		gameTable.put("F1", new Piece("Bishop", true));
		gameTable.put("G1", new Piece("Knight", true));
		gameTable.put("H1", new Piece("Rock", true));
		
		gameTable.put("A2", new Piece("Pawn", true));
		gameTable.put("B2", new Piece("Pawn", true));
		gameTable.put("C2", new Piece("Pawn", true));
		gameTable.put("D2", new Piece("Pawn", true));
		gameTable.put("E2", new Piece("Pawn", true));
		gameTable.put("F2", new Piece("Pawn", true));
		gameTable.put("G2", new Piece("Pawn", true));
		gameTable.put("H2", new Piece("Pawn", true));
		
		gameTable.put("A7", new Piece("Pawn", false));
		gameTable.put("B7", new Piece("Pawn", false));
		gameTable.put("C7", new Piece("Pawn", false));
		gameTable.put("D7", new Piece("Pawn", false));
		gameTable.put("E7", new Piece("Pawn", false));
		gameTable.put("F7", new Piece("Pawn", false));
		gameTable.put("G7", new Piece("Pawn", false));
		gameTable.put("H7", new Piece("Pawn", false));
		
		gameTable.put("A8", new Piece("Rock", false));
		gameTable.put("B8", new Piece("Knight", false));
		gameTable.put("C8", new Piece("Bishop", false));
		gameTable.put("D8", new Piece("Queen", false));
		gameTable.put("E8", new Piece("King", false));
		gameTable.put("F8", new Piece("Bishop", false));
		gameTable.put("G8", new Piece("Knight", false));
		gameTable.put("H8", new Piece("Rock", false));
	}

	public void move(String a, String b) {
		System.out.println("moving a:" + a + ", to b:" + b);
		Piece ap = gameTable.get(a);
		gameTable.put(b, ap);
		gameTable.put(a, null);
	}

	public Piece getPiece(String p) {
		Piece piece = gameTable.get(p);
		if(piece == null) piece = VOID;
		return piece;
	}

	public void move(String commandSet) {
		String[] roundList = commandSet.split(";");
		for (String round : roundList) {
			System.out.println("round: " + round);
			String[] movePairList = round.trim().split(",");
			for (String movePair : movePairList) {
				String[] m = movePair.trim().split(" ");
				move(m[0].trim(),m[1].trim());
			}
		}
		
	}

	public Map<String, Piece> getGameTable() {
		return gameTable;
	}

}
